using System;
using System.Runtime.InteropServices;
using System.Web.UI;
using System.Web.UI.WebControls.WebParts;
using Microsoft.IW;
using System.Configuration;
namespace CSP.WebPartPages.Webpart
{
    [Guid("e5baef73-8538-4ae1-9b14-c1d24bcdd615")]
    public class WhatsOnYourMind : Microsoft.SharePoint.WebPartPages.WebPart
    {
        private bool _error = false;
        private string _myProperty = null;
        private string _peepSite = null;
        private string _peepList = null;
        private string _peepUser = null;
        private string _peepPwd = null;
        private string _CSP = "~/_controltemplates/CSP/WhatsOnYourMind.ascx";

        [Personalizable(PersonalizationScope.Shared)]
        [WebBrowsable(true)]
        [System.ComponentModel.Category("My Property Group")]
        [WebDisplayName("MyProperty")]
        [WebDescription("Meaningless Property")]
        public string MyProperty
        {
            get
            {
                if (_myProperty == null)
                {
                    _myProperty = "Hello SharePoint";
                }
                return _myProperty;
            }
            set { _myProperty = value; }
        }

        [Personalizable(PersonalizationScope.Shared)]
        [WebBrowsable(true)]
        [System.ComponentModel.Category("Peeps")]
        [WebDisplayName("Site")]
        [WebDescription("Site URL to store peeps")]
        public string PeepSite
        {
            get
            {
                if (_peepSite == null)
                {
                    _peepSite = Microsoft.IW.Properties.Settings.Default.InitialPeepSite;
                }
                return _peepSite;
            }
            set { _peepSite = value; }
        }

        [Personalizable(PersonalizationScope.Shared)]
        [WebBrowsable(true)]
        [System.ComponentModel.Category("Peeps")]
        [WebDisplayName("List")]
        [WebDescription("List to store peeps")]
        public string PeepList
        {
            get
            {
                if (_peepList == null)
                {
                    _peepList = Microsoft.IW.Properties.Settings.Default.UserTweetsListName;
                }
                return _peepList;
            }
            set { _peepList = value; }
        }

        [Personalizable(PersonalizationScope.Shared)]
        [WebBrowsable(true)]
        [System.ComponentModel.Category("Peeps")]
        [WebDisplayName("User name")]
        [WebDescription("User who post a peeps")]
        public string PeepUser
        {
            get
            {
                if (_peepUser == null)
                {
                    _peepUser = Microsoft.IW.Properties.Settings.Default.InitialPeepUser;
                }
                return _peepUser;
            }
            set { _peepUser = value; }
        }

        [Personalizable(PersonalizationScope.Shared)]
        [WebBrowsable(true)]
        [System.ComponentModel.Category("Peeps")]
        [WebDisplayName("Twitter user password")]
        [WebDescription("Password for the user")]
        public string PeepPassword
        {
            get
            {
                if (_peepPwd == null)
                {
                    _peepPwd = Microsoft.IW.Properties.Settings.Default.InitialPeepSitePassword;
                }
                return _peepPwd;
            }
            set { _peepPwd = value; }
        }

        public WhatsOnYourMind()
        {
            this.ExportMode = WebPartExportMode.All;
        }

        /// <summary>
        /// Create all your controls here for rendering.
        /// Try to avoid using the RenderWebPart() method.
        /// </summary>
        protected override void CreateChildControls()
        {
            if (!_error)
            {
                try
                {

                    base.CreateChildControls();
                    CSP.WebControls.WhatsOnYourMind woym = (CSP.WebControls.WhatsOnYourMind)this.Page.LoadControl(_CSP);
                    woym.SiteURL = PeepSite;
                    woym.ListName = PeepList;
                    woym.User = PeepUser;
                    woym.Password = PeepPassword;
                    this.Controls.Add(woym);
                }
                catch (Exception ex)
                {
                    HandleException(ex);
                }
            }
        }

        /// <summary>
        /// Ensures that the CreateChildControls() is called before events.
        /// Use CreateChildControls() to create your controls.
        /// </summary>
        /// <param name="e"></param>
        protected override void OnLoad(EventArgs e)
        {
            if (!_error)
            {
                try
                {
                    base.OnLoad(e);
                    this.EnsureChildControls();

                    // Your code here...
                }
                catch (Exception ex)
                {
                    HandleException(ex);
                }
            }
        }

        /// <summary>
        /// Clear all child controls and add an error message for display.
        /// </summary>
        /// <param name="ex"></param>
        private void HandleException(Exception ex)
        {
            this._error = true;
            this.Controls.Clear();
            this.Controls.Add(new LiteralControl(ex.Message));
        }
    }
}
